const app = {
  state: {
    sidebar: {
      opened: !+localStorage.getItem('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop',
    breadcrumb: {
      name: localStorage.getItem('bcName'),
      title: localStorage.getItem('bcTitle'),
      path: localStorage.getItem('bcPath')
    }
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        localStorage.setItem('sidebarStatus', 1)
      } else {
        localStorage.setItem('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      localStorage.setItem('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_BREADCRUMB: (state, breadcrumb) => {
      localStorage.setItem('bcName', breadcrumb.name)
      state.breadcrumb.name = breadcrumb.name
      localStorage.setItem('bcTitle', breadcrumb.meta.title)
      state.breadcrumb.title = breadcrumb.meta.title
      localStorage.setItem('bcPath', breadcrumb.path)
      state.breadcrumb.path = breadcrumb.path
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    breadcrumb({commit}, breadcrumb) {
      commit('SET_BREADCRUMB', breadcrumb)
    }
  }
}

export default app
